# cmake_minimum_required(VERSION 3.0.0)
# project(ink_water VERSION 0.1.0 LANGUAGES C)

# include(CTest)
# enable_testing()

# add_library(ink_water ink_water.c)

# set(CPACK_PROJECT_NAME ${PROJECT_NAME})
# set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
# include(CPack)
# 手动编写编译规则，但是cmake编写很麻烦，选择使用cmakelist进行编写

# 1.定义cmake版本以及工程名称，工程在后面那个name

# 

cmake_minimum_required(VERSION 3.10)  # CMake 最低版本号要求

set(MAKE_DEBUG ON) #dehug时候用，选择等级

if(MAKE_DEBUG)
	set(CMAKE_BUILD_TYPE "debug")
	message("make by Debug")
else()
	set(CMAKE_BUILD_TYPE "Release")
	message("make by Release")
endif()




# 源码-二进制（目标文件）-目标文件与系统组件打包为可执行文件

# set(EXT "")
# 告诉 CMake，当前正在交叉编译，而不是在本地编译。Generic 是一个通用的名称，表示不属于任何特定的操作
# 系统或处理器体系结构。这意味着 CMake 将不会针对任何特定的操作系统或处理器体系结构生成代码，而是生成通
# 用的代码，这可以使得我们的代码更具可移植性。

# 2.定义编译,链接工具。让makefile知到使用gcc进编译
set(CMAKE_SYSTEM_NAME Generic)
# GNU ARM的编译器
set(CMAKE_C_COMPILER "D:/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-gcc.exe")
set(CMAKE_CXX_COMPILER "D:/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-g++.exe")
set(CMAKE_OBJCOPY "D:/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-objcopy.exe")
set(CMAKE_SIZE "D:/gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-size.exe")

set(PROJECT_NAME "InkScreen") 
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/Map)

# 3.设置编译选项 这边修改
# https://blog.csdn.net/fdcp123/article/details/114537469 选项需要修改
set(MCU_FLAGS "-mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16")  # 处理器架构
set(CMAKE_C_FLAGS "${MCU_FLAGS} -Wall -lgcc -lc -lm -std=gnu99 -lrdimon") 
# debug的显示
set(CMAKE_C_FLAGS_DEBUG " -Wall -O0 -g2 -ggdb") 
set(CMAKE_C_FLAGS_RELEASE "-Wall -O2") #02等级比00高
#
set(CMAKE_ASM_FLAGS " -c -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections \
-fdata-sections -x assembler-with-cpp -Wa,-mimplicit-it=thumb  -gdwarf-2")
set(LINKER_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/Lib/Startup/Linker/AT32F403AxC_FLASH.ld)
set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs -specs=rdimon.specs -specs=nosys.specs -T${LINKER_SCRIPT}  \
-lgcc -lc -lm -lrdimon -Wl,-Map=${CMAKE_BINARY_DIR}/Map/${PROJECT_NAME}.map,--cref -Wl,--gc-sections")

# 通过这句会获取到CmakeLists的文件路径 PROJECT_SOURCE_DIR
project(${PROJECT_NAME})

# 4.添加宏定义
## 
add_definitions(
	-DAT32F403ARCT7
	-DUSE_STDPERIPH_DRIVER
)

# 3.添加工程源文件和头文件/路径


include_directories(
	${PROJECT_SOURCE_DIR}/screen
	${PROJECT_SOURCE_DIR}/Segger
	${PROJECT_SOURCE_DIR}/Hardware
	# ${PROJECT_SOURCE_DIR}/drive
	${PROJECT_SOURCE_DIR}/Lib
	${PROJECT_SOURCE_DIR}/Lib/inc
	${PROJECT_SOURCE_DIR}/Lib/Core
	${PROJECT_SOURCE_DIR}/Lib/Startup
	# ${PROJECT_SOURCE_DIR}/Lib/src ##
	# ${PROJECT_SOURCE_DIR}/Lib ## ？ 还有src为什么不添加
)

ENABLE_LANGUAGE(ASM) ##启用汇编语言
set(SRCS_ASM
	${CMAKE_CURRENT_SOURCE_DIR}/Lib/Startup/startup_at32f403a_407.s
	# ${PROJECT_SOURCE_DIR}/Rtthread/libcpu/arm/cortex-m4/context_gcc.S 
)

#aux_source_directory命令会查找指定目录下的所有源文件
#然后将所有源文件的文件名存入DIR_SRCS 
aux_source_directory(${PROJECT_SOURCE_DIR}/screen DIR_SRCS)
# aux_source_directory(${PROJECT_SOURCE_DIR}/drive DIR_SRCS)
aux_source_directory(${PROJECT_SOURCE_DIR}/Lib DIR_SRCS)
aux_source_directory(${PROJECT_SOURCE_DIR}/Lib/src DIR_SRCS)
aux_source_directory(${PROJECT_SOURCE_DIR}/Lib/Startup DIR_SRCS)
aux_source_directory(${PROJECT_SOURCE_DIR}/Segger DIR_SRCS)
aux_source_directory(${PROJECT_SOURCE_DIR}/Hardware DIR_SRCS)

# aux_source_directory(${PROJECT_SOURCE_DIR}/Lib/Core DIR_SRCS) ##
# aux_source_directory(${PROJECT_SOURCE_DIR}/Lib/inc DIR_SRCS) ##




set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/Bin)

# 指定生成目标 ？ 是不是这边问题
add_executable(${PROJECT_NAME}.elf ${DIR_SRCS} ${SRCS_ASM}) #elf文件使用.c与.s构建
# 输出hex、bin文件 通过arm-none-eabi-objcopy
set(ELF_FILE ${PROJECT_BINARY_DIR}/Bin/${PROJECT_NAME}.elf)
set(HEX_FILE ${PROJECT_BINARY_DIR}/Bin/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/Bin/${PROJECT_NAME}.bin)
# CMAKE_SIZE 得到大小信息，并在终端输出
add_custom_command(TARGET "${PROJECT_NAME}.elf" POST_BUILD
	COMMAND ${CMAKE_OBJCOPY} -O binary ${ELF_FILE} ${BIN_FILE}
	COMMAND ${CMAKE_OBJCOPY} -O ihex ${ELF_FILE} ${HEX_FILE}
	COMMENT "Building ${PROJECT_NAME}.bin and ${PROJECT_NAME}.hex"
	COMMAND ${CMAKE_SIZE} --format=berkeley ${ELF_FILE} ${HEX_FILE}
	COMMENT "Invoking: Cross ARM GNU Print Size"
)
set(JLINK "D:/JLINK6.94/JLink.exe")

# 添加下载命令
add_custom_target(flash
	COMMAND ${JLINK} -Device AT32F403ARCT7 -if SWD -speed 4000 -CommandFile download.jlink
	COMMENT "download..."
	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
	COMMENT "ok..."
)

# 把下载命令与编译目标绑定

add_dependencies(flash ${PROJECT_NAME}.elf) # 编译成可执行文件

# 先cmake生成makeFile后就使用make进行编译生成可执行文件
#download jlink 是哪里出现的